<template>
  <div>
    <div class="search">
      <div class="search-left">
        <el-button @click="onAdd">添加</el-button>
        <!-- <el-dropdown
          style="margin-left: 20px"
          size="small"
          split-button
          @command="handleCommand"
        >
          更多操作
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="batchImport">批量导入</el-dropdown-item>
            <el-dropdown-item command="batchDownload"
              >批量下载</el-dropdown-item
            >
            <el-dropdown-item command="batchDeletion"
              >批量删除</el-dropdown-item
            >
          </el-dropdown-menu>
        </el-dropdown> -->
      </div>
      <div class="search-right">
        <template v-for="(v, i) in tableLabel">
          <template v-if="v.isShow">
            <el-select
              v-model="form[v.prop]"
              :placeholder="`请选择${v.name}`"
              v-if="v.type === 'select'"
              clearable
              filterable
              size="small"
            >
              <el-option
                v-for="item in v.option"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <el-input
              size="small"
              clearable
              v-model.trim="form[v.prop]"
              :placeholder="v.text || `输入${v.name}`"
              v-else
              :type="v.type"
            ></el-input>
          </template>
        </template>
      </div>
      <div>
        <el-button type="primary" plain @click="onSearch">搜索</el-button>
        <el-button type="success" plain @click="onRest">重置</el-button>
      </div>
    </div>
    <el-divider></el-divider>
    <el-table border :data="tableData" v-loading="loading">
      <template v-for="(v, i) in tableLabel">
        <el-table-column
          v-if="!v.hidden"
          :key="v.prop"
          :prop="v.prop"
          :label="v.name"
          :width="v.width"
          :show-overflow-tooltip="v.tooltip"
          :align="v.align || 'center'"
        >
          <template #default="scope">
            <div v-if="v.slot">
              <slot :name="v.prop" :data="scope.row" />
            </div>
            <div v-else>
              <span>{{ scope.row[v.prop] }}</span>
            </div>
          </template>
        </el-table-column>
      </template>
      <!-- 自定义列 -->
      <slot name="operate" />
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>
<script setup>
import { ref, onMounted } from "vue";
defineProps({
  tableData: {
    type: Array,
    default: () => [],
  },
  tableLabel: {
    type: Array,
    default: () => [],
  },
  pageSize: {
    type: Number,
    default: 10,
  },
  pageNum: {
    type: Number,
    default: 1,
  },
  total: {
    type: Number,
    default: 0,
  },
  loading: {
    type: Boolean,
    default: false,
  },
});
const emit = defineEmits([
  "onSearch",
  "handleSizeChange",
  "handleCurrentChange",
  "onRest",
  "onAdd",
]);
const form = ref({});
const onSearch = () => {
  emit("onSearch", form.value);
};
const handleSizeChange = (val) => {
  emit("handleSizeChange", val);
};
const handleCurrentChange = (val) => {
  emit("handleCurrentChange", val);
};
const onRest = () => {
  form.value = {};
  emit("onRest");
};
const onAdd = () => {
  emit("onAdd");
};
</script>

<style lang="scss" scoped>
.search {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 20px;
  &-left {
    width: 30%;
  }

  &-right {
    width: 50%;
    display: flex;
    // justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    .el-input {
      margin: 5px;
      width: 28%;
    }
    .el-select {
      width: 28%;
      margin: 5px;
    }
  }
}
.el-pagination {
  margin-top: 20px;
}
</style>